package com.gitee.neuray.security.par.examperiod.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gitee.neuray.security.common.msg.ObjectRestResponse;
import com.gitee.neuray.security.common.msg.TableResultResponse;
import com.gitee.neuray.security.common.rest.BaseController;
import com.gitee.neuray.security.common.util.ExcelUtils;
import com.gitee.neuray.security.common.util.Query;
import com.gitee.neuray.security.par.examperiod.vo.ExamperiodVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.gitee.neuray.security.par.examperiod.biz.ExamperiodBiz;
import com.gitee.neuray.security.par.examperiod.vo.ExamperiodPageExcelVo;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 *
 *
 * @author dev
 * @email ${email}
 * @date 2020-08-31 10:51:24
 */
@RestController
@RequestMapping("examperiod")
public class ExamperiodController extends BaseController<ExamperiodBiz,ExamperiodVo> {
    @Autowired
    private ExamperiodBiz examperiodBiz;


    /**
     * 分页获取列表
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "queryPage", method = RequestMethod.GET)
    public TableResultResponse queryPage(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        return examperiodBiz.page(query);
    }

    @RequestMapping(value = "export", method = RequestMethod.GET)
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        Query query = new Query(params);
        List data = examperiodBiz.nopage(query);
        List<ExamperiodPageExcelVo> execlData = JSONArray.parseArray(JSONObject.toJSONString(data), ExamperiodPageExcelVo.class);
        ExcelUtils.export(response, "测试代码生成",
                execlData,
                ExamperiodPageExcelVo.class);
    }


    /**
     * @description: 新增数据
     * @author: 田昊
     * @date: 2020/8/31
     * @param examperiodVo:
     * @return: com.gitee.neuray.security.common.msg.ObjectRestResponse
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public ObjectRestResponse insert(@RequestBody ExamperiodVo examperiodVo) {
        ObjectRestResponse response = new ObjectRestResponse();
        int checkName = examperiodBiz.checkName(examperiodVo);
        //校验
        if (checkName > 0 ) {
            response.setMessage("考评周期名称不能重复");
            return response.rel(false);
        }
        examperiodBiz.insertSelective(examperiodVo);
        return response.rel(true);
    }

    /**
     * @description: 修改
     * @author: 田昊
     * @date: 2020/8/31
     * @param examperiodVo:
     * @return: com.gitee.neuray.security.common.msg.ObjectRestResponse
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public ObjectRestResponse updateExamperiod(@RequestBody ExamperiodVo examperiodVo) {
        ObjectRestResponse response = new ObjectRestResponse();
        int checkName = examperiodBiz.checkName(examperiodVo);
        //校验
        if (checkName > 0 ) {
            response.setMessage("考评周期名称不能重复");
            return response.rel(false);
        }
        examperiodBiz.updateById(examperiodVo);
        return response.rel(true);
    }

    /**
     * @description: 删除，删除前校验PLAN表是否有引用
     * @author: 田昊
     * @date: 2020/8/31
     * @param examperiodVo:
     * @return: com.gitee.neuray.security.common.msg.ObjectRestResponse
     */
    @RequestMapping(value = "delete", method = RequestMethod.DELETE)
    public ObjectRestResponse delete(@RequestBody ExamperiodVo examperiodVo) {
        ObjectRestResponse response = new ObjectRestResponse();
        int checkPlan = examperiodBiz.checkPlan(examperiodVo);
        //校验
        if (checkPlan > 0){
            response.setMessage("该周期已经开始使用，请先删除对应的考评计划，或可以将此周期置为无效状态");
            return response.rel(false);
        }
        examperiodBiz.delete(examperiodVo);
        return response.rel(true);
    }
}
